using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    public partial class BFileDownloadEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private BFileDownloadComponent _BFileDownloader;

        public BFileDownloadEditor()
        {
            InitializeComponent();
        }

        private void BFileDownloadEditor_Load(object sender, EventArgs e)
        {
            this.PopulateDbProviders();
        }

        private void PopulateDbProviders()
        {
            this.cbo_DbProvider.Items.Clear();
            for(int i=0;i<=2;i++)
            {
                DataProviderType dbProvider = (DataProviderType) i;
                this.cbo_DbProvider.Items.Add(dbProvider.ToString());
            }
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(BFileDownloadComponent); }
        }

        public void LoadSettings(
            GlobalVariable[] globalVars,
            WorkflowComponentBase[] fromComponents, 
            object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._BFileDownloader = (BFileDownloadComponent) wfComponent;

            this.cbo_DbProvider.SelectedIndex = (int) this._BFileDownloader.DbProvider;
            this.txt_ConnStr.Text = this._BFileDownloader.ConnStr;
            if (!string.IsNullOrEmpty(this._BFileDownloader.ConnStr))
            {
                this.btn_Refresh_Click(this, new EventArgs());
                this.cbo_BFileField.SelectedText = this._BFileDownloader.BFileField;
                this.cbo_FileNameField.SelectedText = this._BFileDownloader.FileNameField;
            }
            this.txt_SelectSQL.Text = this._BFileDownloader.SelectStatement;
            this.txt_DownloadTo.Text = this._BFileDownloader.DownloadToFolder;
            this.txt_FileNameFormat.Text = this._BFileDownloader.FileNameFormat;
            this.txt_LogFile.Text = this._BFileDownloader.LogFilePath;
            this.txt_ErrorFile.Text = this._BFileDownloader.ErrorFilePath;
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
                this.txt_DownloadTo.Text = dlg.SelectedPath;
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            DataProviderType dbProvider = (DataProviderType) this.cbo_DbProvider.SelectedIndex;
            TableColumn[] tblFields = DbUtil.GetTableColumns(dbProvider, this.txt_ConnStr.Text, this.txt_SelectSQL.Text);
            this.cbo_FileNameField.Items.Clear();
            this.cbo_BFileField.Items.Clear();
            if(tblFields !=null && tblFields.Length>0)
            {
                foreach(TableColumn col in tblFields)
                {
                    this.cbo_BFileField.Items.Add(col.ColumnName);
                    this.cbo_FileNameField.Items.Add(col.ColumnName);
                }
            }
        }

        public TestResult Test()
        {
            return new TestResult(true, string.Empty);
        }

        public void SaveSettings()
        {
            this._BFileDownloader.DbProvider = (DataProviderType) this.cbo_DbProvider.SelectedIndex;
            this._BFileDownloader.ConnStr = this.txt_ConnStr.Text;
            this._BFileDownloader.SelectStatement = this.txt_SelectSQL.Text;
            this._BFileDownloader.BFileField = this.cbo_BFileField.Text;
            this._BFileDownloader.FileNameField = this.cbo_FileNameField.Text;
            this._BFileDownloader.DownloadToFolder = this.txt_DownloadTo.Text;
            this._BFileDownloader.FileNameFormat = this.txt_FileNameFormat.Text;
            this._BFileDownloader.LogFilePath = this.txt_LogFile.Text;
            this._BFileDownloader.ErrorFilePath = this.txt_ErrorFile.Text;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent!=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._BFileDownloader);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            BFileDownloadInstruction instruction =
                new BFileDownloadInstruction(this._BFileDownloader.ComponentID, Guid.Empty, 
                    this._BFileDownloader.DbProvider,
                                             this._BFileDownloader.ConnStr, this._BFileDownloader.BFileField,
                                             this._BFileDownloader.FileNameField, this._BFileDownloader.SelectStatement,
                                             this._BFileDownloader.DownloadToFolder, this._BFileDownloader.FileNameFormat,
                                             this._BFileDownloader.LogFilePath, this._BFileDownloader.ErrorFilePath);
            BFileDownloadWorker worker=new BFileDownloadWorker();
            worker.SetInstruction(Guid.NewGuid(),instruction, string.Empty, this._BFileDownloader.ComponentName);
            worker.WorkerErredEvent += new WorkerHasError(worker_WorkerErredEvent);
            worker.WorkerFinishedEvent += new WorkerFinished(worker_WorkerFinishedEvent);
            worker.DoWork();
        }

        void worker_WorkerFinishedEvent(Guid ticketID, Type workerType, InstructionBase instructionBase, ResultBase resultBase)
        {
            MessageBox.Show("Done");
        }

        void worker_WorkerErredEvent(Guid ticketID, Type workerType, InstructionBase instructionBase, string error, bool isFatal)
        {
            MessageBox.Show(error);
        }

        private void btn_Browse2_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg=new SaveFileDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_LogFile.Text = dlg.FileName;
            }
        }

        private void btn_Browse3_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_ErrorFile.Text = dlg.FileName;
            }
        }
    }
}